package com.donfu.web.controller.house;

import java.util.*;

import com.donfu.common.utils.VideoUtil;
import com.donfu.framework.config.ServerConfig;
import com.donfu.system.domain.dto.FileChunkDTO;
import com.donfu.system.domain.dto.FileChunkResultDTO;
import com.donfu.system.domain.dto.RestApiResponse;
import com.donfu.system.domain.house.HouseAboutModen;
import com.donfu.system.service.house.IHouseAboutModenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.donfu.common.annotation.Log;
import com.donfu.common.core.controller.BaseController;
import com.donfu.common.core.domain.AjaxResult;
import com.donfu.common.enums.BusinessType;
import com.donfu.common.utils.poi.ExcelUtil;
import com.donfu.common.core.page.TableDataInfo;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpServletResponse;

/**
 * 关于摩登文章Controller
 * 
 * @author donfu
 * @date 2021-12-08
 */
@Api(tags = "后台关于摩登")
@RestController
@RequestMapping("/house/aboutModen")
public class HouseAboutModenController extends BaseController
{
    @Autowired
    private IHouseAboutModenService houseAboutModenService;

    @Autowired
    private ServerConfig serverConfig;



    /**
     * 查询关于摩登文章列表
     */
    @ApiOperation("查询列表")
    @PreAuthorize("@ss.hasPermi('house:moden:list')")
    @GetMapping("/list")
    public TableDataInfo list()
    {
        startPage();
        List<HouseAboutModen> list = houseAboutModenService.selectHouseAboutModenList(null);
        return getDataTable(list);
    }

    /**
     * 查询具体的文章信息，用于回显
     * @param id
     * @return
     */
    @ApiOperation("查询具体文章信息")
    @GetMapping("/getInfoById/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        /**
         * 业务层已转译
         */
        HouseAboutModen houseAboutModen = houseAboutModenService.selectHouseAboutModenById(id);
        return AjaxResult.success(houseAboutModen);
    }

    /**
     * 新增和摩登翡丽相关的文章
     * @param houseAboutModen
     * @return
     */
    @ApiOperation("新增文章")
    @PostMapping("/add")
    public AjaxResult addHouseAboutModen(@RequestBody HouseAboutModen houseAboutModen){
        AjaxResult ajaxResult = checkHouseAboutModen(houseAboutModen);
        String code = ajaxResult.get("code").toString();
        if(code.contains("50")){
            return ajaxResult;
        }
        int i = houseAboutModenService.insertHouseAboutModen(houseAboutModen);
        return toAjax(i);
    }


    /**
     * 修改关于摩登文章
     */
    @ApiOperation("修改文章")
    @PreAuthorize("@ss.hasPermi('house:moden:edit')")
    @Log(title = "关于摩登文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseAboutModen houseAboutModen)
    {
        if(houseAboutModen.getId()==null){
            return AjaxResult.error("id不能为空");
        }
        AjaxResult ajaxResult = checkHouseAboutModen(houseAboutModen);
        String code = ajaxResult.get("code").toString();
        if(code.contains("50")){
            return ajaxResult;
        }
        return toAjax(houseAboutModenService.updateHouseAboutModen(houseAboutModen));
    }

    /**
     * 删除文章
     * @param ids
     * @return
     */
    @ApiOperation("删除文章")
    @DeleteMapping("/{ids}")
    public AjaxResult deleteInfo(@PathVariable("ids") Long[] ids){
        int i = houseAboutModenService.deleteHouseAboutModenByIds(ids);
        return toAjax(i);
    }

    /**
     * 检测参数是否合格
     * @param houseAboutModen
     * @return
     */
    public AjaxResult checkHouseAboutModen(HouseAboutModen houseAboutModen){
        String title = houseAboutModen.getTitle();
        if(title==null || title.equals("")){
            return AjaxResult.error("标题不能为空");
        }
        if(title.length()>150){
            return AjaxResult.error("标题名称过长");
        }
        String content = houseAboutModen.getContent();
        if (content==null || content.equals("")){
            return AjaxResult.error("文章内容不能为空");
        }
        String standby = houseAboutModen.getStandby();
        if(standby!=null && standby.length()>100){
            return AjaxResult.error("备注过长");
        }
        String typeName = houseAboutModen.getTypeName();
        if(typeName==null ||  typeName.equals("")){
            return AjaxResult.error("类型必须要选");
        }
        String coverImgUrl = houseAboutModen.getCoverImgUrl();
        if(coverImgUrl==null || coverImgUrl.equals("")){
            return  AjaxResult.error("封面必须选择");
        }
        return AjaxResult.success();
    }

    ///**
    // * 检查分片是否存在
    // * @return
    // */
    //@ApiOperation("检测分片是否存在")
    //@GetMapping(value = "/upload/chunk")
    //public AjaxResult checkChunkExist(FileChunkDTO chunkDTO, HttpServletResponse response) {
    //    FileChunkResultDTO fileChunkCheckDTO;
    //    //.avi .rmvb .rm .mp4 .flv .mpg
    //    String ext = chunkDTO.getFilename().substring(chunkDTO.getFilename().lastIndexOf("."));
    //    if (ext.equals(".avi")||ext.equals(".rmvb")||ext.equals(".rm")||ext.equals(".mp4")||ext.equals(".flv")||ext.equals(".mpg"))
    //    {
    //        try {
    //            fileChunkCheckDTO = houseAboutModenService.checkChunkExist(chunkDTO);
    //            return AjaxResult.success( fileChunkCheckDTO);
    //        } catch (Exception e) {
    //            return AjaxResult.error("检查分片存在错误");
    //        }
    //    }
    //    return AjaxResult.error("请上传视频文件");
    //}
    //
    //
    ///**
    // * 上传文件分片
    // * @param chunkDTO
    // * @return
    // */
    //@ApiOperation("上传文件分片")
    //@PostMapping(value = "/upload/chunk")
    //public RestApiResponse<Object> uploadChunk(FileChunkDTO chunkDTO, HttpServletResponse response)  {
    //    try {
    //        houseAboutModenService.uploadChunk(chunkDTO);
    //        Map<String,Object> map = new HashMap<>();
    //        String videoPath = getFileAbsolutePath(chunkDTO.getIdentifier(), chunkDTO.getFilename());
    //        String imgPath = (videoPath.substring(0,videoPath.lastIndexOf("."))) + ".jpg" ;
    //        //视频截图
    //        Map<String, Object> screenshot = VideoUtil.getScreenshot(chunkDTO.getFilePath());
    //        map.put("videoPath",videoPath);
    //        map.put("imgPath",imgPath);
    //        return RestApiResponse.success(map);
    //    } catch (Exception e) {
    //        return RestApiResponse.error("close");
    //    }
    //}
    //
    ///**
    // * 设置上传错误响应
    // * @param response
    // * @param e
    // * @return
    // */
    //private Map<String, Object> getErrorMap(HttpServletResponse response, Exception e) {
    //    response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    //    Map<String, Object> error = new HashMap<>();
    //    error.put("errorMsg", e.getMessage());
    //    error.put("errorCode", HttpStatus.INTERNAL_SERVER_ERROR.value());
    //    return error;
    //}
    //
    //
    ///**
    // * 得到文件的相对路径
    // *
    // * @param identifier
    // * @param filename
    // * @return
    // */
    //public String getFileRelativelyPath(String identifier, String filename) {
    //    Calendar cal = Calendar.getInstance();
    //    int year = cal.get(Calendar.YEAR);//获取年份
    //    int month=cal.get(Calendar.MONTH) + 1;//获取月份
    //    int day=cal.get(Calendar.DATE);//获取日
    //    String ext = filename.substring(filename.lastIndexOf("."));
    //    return "/profile/"+"video/" + year + "/" +
    //            month + "/" +
    //            day + "/" +
    //            identifier + "/" + identifier
    //            + ext;
    //}
    //
    ///**
    // * 得到文件的绝对路径
    // *
    // * @param identifier
    // * @param filename
    // * @return
    // */
    //public String getFileAbsolutePath(String identifier, String filename) {
    //    Calendar cal = Calendar.getInstance();
    //    int year = cal.get(Calendar.YEAR);//获取年份
    //    int month=cal.get(Calendar.MONTH) + 1;//获取月份
    //    int day=cal.get(Calendar.DATE);//获取日
    //    String ext = filename.substring(filename.lastIndexOf("."));
    //    return serverConfig.getUrl()+"/profile/"+"video/" + year + "/" +
    //            month + "/" +
    //            day + "/" +
    //            identifier + "/" + identifier
    //            + ext;
    //}

}
